# -*- coding: utf-8 -*-
# created on 2016/11/30
# 

from mathsolver.functions.base import *
from sympy import symbols


class ZXJudgePointAxis(BaseFunction):
    def solver(self, *args):
        x0, y0 = args[0].sympify()
        if x0 != 0 and y0 != 0:
            if x0 > 0 and y0 > 0:
                xx = 1
            elif x0 < 0 < y0:
                xx = 2
            elif x0 < 0 and y0 < 0:
                xx = 3
            elif x0 > 0 > y0:
                xx = 4
            else:
                raise Exception("to do")
            self.steps.append(["", "∴ %s在第%s象限" % (BasePoint({"name": "", "value": [x0, y0]}).printing(), xx)])
            self.output.append(BaseValue(xx))
        elif y0 == 0:
            if x0 > 0:
                self.steps.append(["", "∴ %s在x轴正半轴上" % BasePoint({"name": "", "value": [x0, y0]}).printing()])
                self.output.append(BaseString("x轴正半轴"))
            elif x0 < 0:
                self.steps.append(["", "∴ %s在x轴负半轴上" % BasePoint({"name": "", "value": [x0, y0]}).printing()])
                self.output.append(BaseString("x轴负半轴"))
            elif x0 == 0:
                self.steps.append(["", "∴ %s为原点" % BasePoint({"name": "", "value": [x0, y0]}).printing()])
                self.output.append(BaseString("原点"))
        elif x0 == 0:
            if y0 > 0:
                self.steps.append(["", "∴ %s在y轴正半轴上" % BasePoint({"name": "", "value": [x0, y0]}).printing()])
                self.output.append(BaseString("y轴正半轴"))
            elif y0 < 0:
                self.steps.append(["", "∴ %s在y轴负半轴上" % BasePoint({"name": "", "value": [x0, y0]}).printing()])
                self.output.append(BaseString("y轴负半轴"))
        self.label.add("判断点所在位置")
        return self


# 点所在象限，求参
class ZXPointOnAxis(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BaseSymbolValue):
            jies = args[0].sympify()
            left = jies[x]
            left_value = left.args[0]
            right = jies[y]
            right_value = right.args[0]
            point = [left_value, right_value]
        else:
            point = args[0].sympify()
        if "第一" in args[1]:
            ineqs = [[point[0], ">", S.Zero], [point[1], ">", S.Zero]]
            self.steps.append(["", "∵ (%s, %s) 在第一象限，则" % (new_latex(point[0]), new_latex(point[1]))])
            self.steps.append(["", BaseIneqs(ineqs).printing()])
            self.output.append(BaseIneqs(ineqs))
        elif "第二" in args[1]:
            ineqs = [[point[0], "<", S.Zero], [point[1], ">", S.Zero]]
            self.steps.append(["", "∵ (%s, %s) 在第二象限，则" % (new_latex(point[0]), new_latex(point[1]))])
            self.steps.append(["", BaseIneqs(ineqs).printing()])
            self.output.append(BaseIneqs(ineqs))
        elif "第三" in args[1]:
            ineqs = [[point[0], "<", S.Zero], [point[1], "<", S.Zero]]
            self.steps.append(["", "∵ (%s, %s) 在第二象限，则" % (new_latex(point[0]), new_latex(point[1]))])
            self.steps.append(["", BaseIneqs(ineqs).printing()])
            self.output.append(BaseIneqs(ineqs))
        elif "第四" in args[1]:
            ineqs = [[point[0], ">", S.Zero], [point[1], "<", S.Zero]]
            self.steps.append(["", "∵ (%s, %s) 在第二象限，则" % (new_latex(point[0]), new_latex(point[1]))])
            self.steps.append(["", BaseIneqs(ineqs).printing()])
            self.output.append(BaseIneqs(ineqs))
        self.label.add("点所在象限的条件")
        return self


# 求两点的距离
class ZXGetLiangDianJuLi(BaseFunction):
    def solver(self, *args):
        if len(args) == 1 and isinstance(args[0], BasePoints):
            points = args[0].sympify()
            point1 = points[0]
            point2 = points[1]
        elif len(args) == 1 and isinstance(args[0], BaseLine):
            left, right = args[0].name
            point1 = self.search(left)
            point1 = point1.sympify()
            point2 = self.search(right)
            point2 = point2.sympify()
        elif len(args) == 2:
            point1 = args[0].sympify()
            point2 = args[1].sympify()
        else:
            raise Exception("to do")
        liangdianjuli = sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
        self.steps.append(["", "由两点距离公式知，%s 和 %s 的距离为 %s" % (
            BasePoint({"name": "", "value": [point1[0], point1[1]]}).printing(),
            BasePoint({"name": "", "value": [point2[0], point2[1]]}).printing(),
            new_latex(liangdianjuli))])
        self.label.add("求两点距离")
        self.output.append(BasePoly(liangdianjuli))
        return self


# 点在图像上，代入方程
class ZXPointOnGraph(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BaseSymbolValue):
            jies = args[0].sympify()
            left = jies[x]
            left_value = left.args[0]
            right = jies[y]
            right_value = right.args[0]
            point = [left_value, right_value]
        else:
            point = args[0].sympify()
        eq = args[1].sympify()
        f = (eq[0] - eq[1]).expand().simplify()
        self.steps.append(["", "∵点%s在直线%s上" % (BasePoint({"name": "", "value": [point[0], point[1]]}).printing(), BaseEq(eq).printing())])
        xx, yy = symbols("xx,yy")
        new_f1 = f.subs({x: xx, y: yy})
        new_f = new_f1.subs({xx: point[0], yy: point[1]})
        self.steps.append(["", "∴ %s" % BaseEq([new_f, S.Zero]).printing()])
        self.output.append(BaseEq([new_f, S.Zero]))
        self.label.add("点在图像上，代入方程")
        return self
